package thread.threadpool.example3;

import thread.threadpool.example2.MyRunnable;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 测试ScheduleExecutorService接口中的延迟执行任务和重复执行任务的功能
 * scheduleAtFixedRate(线程类,初始等待时间,执行任务间隔时间,时间单位)
 * @date: 2023/1/31 11:12
 * @author: Huangjunjie
 * @since JDK 1.8
*/
public class ScheduleExecutorServiceDemo02 {
    public static void main(String[] args) {
        //1.获取一个具备延迟执行任务的线程池对象
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3, Thread::new);
        //2.创建多个任务对象，提交任务，每个任务延迟2秒执行        //初始等待1秒，执行任务间隔2秒
        //会重复执行任务
//        pool.scheduleAtFixedRate(new MyRunnable(1),1,3, TimeUnit.SECONDS);
        for (int i = 0; i < 10; i++) {
            pool.scheduleAtFixedRate(new MyRunnable(i),1,1, TimeUnit.SECONDS);
        }
        System.out.println("调度结束");
    }
}
